package com.codbking.calendar.exaple.richeditor;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.codbking.calendar.exaple.R;

public class ColorPickerDialog extends Dialog {
    private RecyclerView recyclerView;
    private ColorPickerView colorPickerView;
    private TextView positive;
    private TextView negative;
    private int color;
    private int position;
    private static int optionalColor = 0;

    public ColorPickerDialog(@NonNull Context context) {
        super(context, R.style.ColorPickerDialog);
        setContentView(R.layout.dialog_color_picker);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        setCanceledOnTouchOutside(false);
        recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
        colorPickerView = (ColorPickerView) findViewById(R.id.color_picker);
        positive = (TextView) findViewById(R.id.positive);
        negative = (TextView) findViewById(R.id.negative);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 5));
        recyclerView.setAdapter(new ColorAdapter());

        colorPickerView.setOnSelectListener(new ColorPickerView.OnSelectListener() {
            @Override
            public void onSelectListener(int color) {
                ((CircleView) recyclerView.getChildAt(recyclerView.getAdapter().getItemCount() - 1).findViewById(R.id.color_view)).setColor(color);
            }
        });

        positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ColorPickerDialog.this.color != optionalColor) {
                    onButtonClickListener.onPositiveListener(ColorPickerDialog.this, ColorPickerDialog.this.color, ColorPickerDialog.this.position);
                } else {
                    onButtonClickListener.onPositiveListener(ColorPickerDialog.this, colorPickerView.getColor(), ColorPickerDialog.this.position);
                }
            }
        });

        negative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onButtonClickListener.onNegativeListener(ColorPickerDialog.this);
            }
        });
    }

    public void setPosition(int position) {
        this.position = position;
    }

    private class ColorAdapter extends RecyclerView.Adapter<ViewHolder> {
        private int[] colors;
        private boolean[] selectMark;

        ColorAdapter() {
            colors = new int[]{0xffff0000, 0xffffff00, 0xffb1f2a1, 0xffa1e8f2, 0xffBEBEBE, 0xffFFDEAD, 0xffFF7F00, 0xffffffff, 0xff000000, optionalColor};
            selectMark = new boolean[colors.length];
            selectMark[ColorPickerDialog.this.position] = true;
            ColorPickerDialog.this.color = colors[0];
            ColorPickerDialog.this.position = 0;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_color_picker, null);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            holder.view.setColor(colors[position]);
            if (position <= colors.length - 2) {
                holder.view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        holder.view.select(true);
                        for (int i = 0; i < selectMark.length; i++) {
                            selectMark[i] = false;
                        }
                        ColorPickerDialog.this.color = colors[holder.getAdapterPosition()];
                        ColorPickerDialog.this.position = holder.getAdapterPosition();
                        selectMark[holder.getAdapterPosition()] = true;
                        colorPickerView.setShowCircle(false);
                        notifyDataSetChanged();
                    }
                });
            } else if (position == colors.length - 1) {
                holder.view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        holder.view.select(true);
                        for (int i = 0; i < selectMark.length; i++) {
                            selectMark[i] = false;
                        }
                        ColorPickerDialog.this.color = colors[holder.getAdapterPosition()];
                        ColorPickerDialog.this.position = holder.getAdapterPosition();
                        selectMark[holder.getAdapterPosition()] = true;
                        colorPickerView.setShowCircle(true);
                        notifyDataSetChanged();
                    }
                });

            }
            if (selectMark[position]) {
                holder.view.select(true);
                if (position == colors.length - 1) {
                    colorPickerView.setShowCircle(true);
                    ColorPickerDialog.this.position = holder.getAdapterPosition();
                }
            } else {
                holder.view.select(false);
            }
        }

        @Override
        public int getItemCount() {
            return colors.length;
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        private CircleView view;

        ViewHolder(View itemView) {
            super(itemView);
            view = (CircleView) itemView.findViewById(R.id.color_view);
        }
    }

    private OnButtonClickListener onButtonClickListener = new OnButtonClickListener() {
        @Override
        public void onPositiveListener(ColorPickerDialog colorPickerDialog, int color, int position) {

        }

        @Override
        public void onNegativeListener(ColorPickerDialog colorPickerDialog) {

        }
    };

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    public interface OnButtonClickListener {
        void onPositiveListener(ColorPickerDialog colorPickerDialog, int color, int position);

        void onNegativeListener(ColorPickerDialog colorPickerDialog);
    }
}
